<template>
  <div>
    <h1>教师个人信息</h1>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item>
        <template slot="label"> 姓名 </template>
        {{ msg.teacher_name }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 职称 </template>
        {{ msg.teacher_Workplace }}
      </el-descriptions-item>
      <!-- <el-descriptions-item>
        <template slot="label"> 学科 </template>
        {{ msg.teacher_subject }}
      </el-descriptions-item> -->
      <el-descriptions-item>
        <template slot="label"> 所属学院 </template>
        {{ msg.teacher_college }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 电话 </template>
        {{ msg.teacher_phone }}
      </el-descriptions-item>
      <el-descriptions-item>
        <template slot="label"> 邮箱 </template>
        {{ msg.teacher_email }}
      </el-descriptions-item>
    </el-descriptions>
    <div class="btn">
      <el-button @click="update_info">修改信息</el-button>
      <el-button @click="submit">返回</el-button>
    </div>

    <!-- 修改信息弹框 -->
    <el-dialog title="修改信息" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="true_updateInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.get_info();
  },
  data() {
    return {
      form: {},
      rules: {
        phone: { min: 11, max: 11, message: "电话长度为11位", trigger: "blur" },
        email: {
          type: "email",
          message: "请输入正确邮箱",
          trigger: ["blur", "change"],
        },
      },
      dialogVisible: false,
      msg: {},
    };
  },
  methods: {
    submit() {
      this.$router.go(-1);
    },

    update_info() {
      this.dialogVisible = true;
    },
    true_updateInfo() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.$http
          .post(
            "http://127.0.0.1:8080/teacher/update_teacherInfo",
            this.$qs.stringify(this.form)
          )
          .then((res) => {
            if (res.data.status == 0) {
              (this.form = {}),
                this.$message.success(res.data.message),
                (this.dialogVisible = false);
              this.get_info();
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
    },

    get_info() {
      this.$http
        .get("http://127.0.0.1:8080/teacher/get_teacherInfo")
        .then((res) => {
          this.msg = res.data.data[0];
          // console.log(res);
        })
        .catch((err) => {
          this.message.error(err);
        });
    },
  },
};
</script>
<style lang="less" scoped>
.btn {
  float: right;
  margin: 20px 0;
}
</style>
